home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / unix / textmstr.shr / tm.hqx / Source Code ƒ / P / Main_TextMaster.c next >
Text File  |  1991-05-09  |  18KB  |  454 lines

  1. /* Main_TextMaster */
  2.  
  3. /* Program name:  Main_TextMaster.c   */
  4. /* Function:  This is the main module for this program.   */
  5. /* History: 5/9/91 Original by Prototyper 3.0   */
  6.  
  7. #include "PCommonTextMaster.h"    /* Common */
  8. #include "Common_TextMaster.h"    /* Common */
  9. #include "PUtils_TextMaster.h"    /* General Utilities */
  10. #include "Utils_TextMaster.h"    /* General Utilities */
  11.  
  12. #include "InitExitTextMaster.h"    /* Init, Exit handlers */
  13. #include "EventsTextMaster.h"    /* Extra event handlers */
  14.  
  15. #include "PD_About_Dialog.h"    /* Modal Dialog */
  16. #include "PD_Help_window.h"    /* Modal Dialog */
  17. #include "PD_Help_window_2.h"    /* Modal Dialog */
  18. #include "PIMenu_TextMaster.h"    /* Init menus */
  19. #include "PDoMenuTextMaster.h"    /* Handle menus */
  20.  
  21. Boolean        DoIt;                                                            /* Flag saying an event is ready */
  22. short    code;                                                                 /* Determine event type */
  23. WindowPtr    whichWindow;                                                /* See which window for event */
  24. long    mResult;                                                              /* Menu list and item selected values */
  25. short    theMenu,theItem;                                                  /* Menu list and item selected */
  26.  
  27. /* Prototypes */
  28.  
  29. /* See if WaitNextEvent is available */
  30. static Boolean WNEIsImplemented(void);
  31.  
  32. /* Check for user events */
  33. static void Handle_User_Event(void);
  34.  
  35. /* Handle key strokes */
  36. static void DoKeyEvent(void);
  37.  
  38. /* Handle a diskette inserted */
  39. static void DoDiskEvent(void);
  40.  
  41. /* Handle a window being resized */
  42. static void DoGrow(WindowPtr whichWindow);
  43.  
  44. /* Handle a window being dragged */
  45. static void DoDrag(WindowPtr whichWindow);
  46.  
  47. /* Handle a window goaway box */
  48. static void DoGoAway(WindowPtr whichWindow);
  49.  
  50. /* Handle a hit in the window */
  51. static void DoInContent(WindowPtr whichWindow);
  52.  
  53. /* Handle an update to the window */
  54. static void DoUpdate(void);
  55.  
  56. /* Handle an activate of the window */
  57. static void DoActivate(void);
  58.  
  59.  
  60.  
  61. /* MAIN entry point */
  62. void main(void);
  63.  
  64.  
  65.  
  66. /* ======================================================= */
  67.  
  68. /* Routine: WNEIsImplemented */
  69. /* Purpose: See if the MultiFinder trap, WaitNextEvent, is available */
  70.  
  71. Boolean WNEIsImplemented()                                               /* See if WaitNextEvent is available */
  72. {
  73. #define    WNETrapNumber    0xA860                                    /* The expected trap number */
  74. SysEnvRec    theWorld;                                                     /* Environment record */
  75. OSErr    discardError;                                                      /* Error code returned */
  76. Boolean    theWNEIsImplemented;                                         /* Value to return */
  77.  
  78. HasColorQD = FALSE;                                                       /* Init to no color QuickDraw */
  79. HasFPU = FALSE;                                                             /* Init to no floating point chip */
  80. InTheForeground = TRUE;                                                   /* Init to a foreground app */
  81. discardError = SysEnvirons(1, &theWorld);                            /* Check how old this system is */
  82. if (theWorld.machineType < 0)                                             /* Negative means really old */
  83.     theWNEIsImplemented = FALSE;                                        /* Really old ROMs, no WNE possible */
  84. else
  85.     {
  86.     theWNEIsImplemented = TrapAvailable(WNETrapNumber, ToolTrap);/* See if trap is there */
  87.     HasColorQD = theWorld.hasColorQD;                                  /* Flag for Color QuickDraw being available */
  88.     HasFPU = theWorld.hasFPU;                                             /* Flag for Floating Point Math Chip being available */
  89.     }
  90. return(theWNEIsImplemented);
  91. }
  92.  
  93. /* ======================================================= */
  94.  
  95. /* Routine: Handle_User_Event */
  96. /* Purpose: Check for user events */
  97.  
  98. void Handle_User_Event()                                                 /* Check for user events */
  99. {
  100. UserEventRec    TheUserEvent;                                            /* The user event */
  101.  
  102. GetUserEvent(&TheUserEvent);                                           /* Check for any user events */
  103. if (TheUserEvent.ID != UserEvent_None)                                 /* Only do if we have any */
  104.     {
  105.  
  106.     switch (TheUserEvent.ID)                                               /* Key off the Event ID */
  107.         {
  108.         case UserEvent_Open_Window:                                      /* Open a Window or Modeless dialog */
  109.             switch (TheUserEvent.ID2)                                       /* Do the appropiate window */
  110.                 {
  111.                 case Res_D_About_Dialog:
  112.                     PD_About_Dialog();                                         /* Open this modal dialog */
  113.                     break;
  114.                 case Res_D_Help_window:
  115.                     PD_Help_window();                                         /* Open this modal dialog */
  116.                     break;
  117.                 case Res_D_Help_window_2:
  118.                     PD_Help_window_2();                                      /* Open this modal dialog */
  119.                     break;
  120.                 default:                                                           /* Handle others */
  121.                     break;
  122.                 }                                                                   /* End of the switch */
  123.             break;
  124.  
  125.         case UserEvent_Close_Window:                                     /* Close a Window or Modeless dialog */
  126.             break;
  127.         default:                                                                 /* Not standard, must be program specific */
  128.             Handle_UserEvent(&TheUserEvent);                            /* Let program specific handle it */
  129.             break;
  130.         }                                                                         /* End of switch */
  131.  
  132.         }                                                                         /* End of handling a user event */
  133. }
  134.  
  135. /* ======================================================= */
  136.  
  137. /* Routine: DoKeyEvent */
  138. /* Purpose: Handle a key pressed */
  139.  
  140. void DoKeyEvent()                                                           /* Handle key presses */
  141. {
  142. short    charCode;                                                           /* Key code */
  143. char    ch;                                                                     /* Key pressed in Ascii */
  144. long    mResult;                                                              /* Menu list and item, if a command key */
  145. short    theMenu,theItem;                                                  /* Menu list and item, if command key */
  146.  
  147. if (HandleKey(&myEvent))                                                  /* Allow for special key handling */
  148.     {
  149.  
  150.     charCode = myEvent.message & charCodeMask;                     /* Get the character */
  151.     ch = (char)charCode;                                                     /* Change it to ASCII */
  152.  
  153.     if ((myEvent.modifiers / cmdKey) & 1)                              /* See if Command key is down */
  154.         {
  155.         mResult = MenuKey(ch);                                             /* See if a menu selection */
  156.         theMenu = HiWord(mResult);                                        /* Get the menu list number */
  157.         theItem = LoWord(mResult);                                         /* Get the menu item number */
  158.         if (theMenu != 0)                                                      /* See if a list was selected */
  159.             Handle_My_Menu(theMenu, theItem);                          /* Do the menu selection */
  160.  
  161.         if (((ch == 'x') || (ch == 'X')) && (theInput != NIL))              /* See if a standard Cut */
  162.             TECut(theInput);                                                    /* Handle a Cut in a TE area */
  163.         if (((ch == 'c') || (ch == 'C')) && (theInput != NIL))              /* See if a standard Copy */
  164.             TECopy(theInput);                                                 /* Handle a Copy in a TE area */
  165.         if (((ch == 'v')  ||  (ch == 'V')) && (theInput != NIL))             /* See if a standard Paste */
  166.             TEPaste(theInput);                                                 /* Handle a Paste in a TE area */
  167.         }                                                                         /* End of Command key special condition */
  168.     else if (theInput != NIL)
  169.         TEKey(ch,theInput);                                                  /* Place the normal key stroke */
  170.  
  171.     }                                                                             /* End of Standard keystroke handling */
  172. }
  173.  
  174. /* ======================================================= */
  175.  
  176. /* Routine: DoDiskEvent */
  177. /* Purpose: Handle a diskette inserted */
  178.  
  179. void DoDiskEvent()                                                          /* Handle disk inserted */
  180. {
  181. short    theError;                                                            /* Error returned from mount */
  182.  
  183. if (HandleDisk(&myEvent))                                                 /* Allow for special disk inserted handling */
  184.     {
  185.  
  186.     if (HiWord(myEvent.message) != noErr)                              /* See if a diskette mount error */
  187.         {                                                                         /* due to unformatted diskette inserted */
  188.         myEvent.where.h = ((screenBits.bounds.right - screenBits.bounds.left) / 2) - (304 / 2);/* Center horz */
  189.         myEvent.where.v = ((screenBits.bounds.bottom - screenBits.bounds.top) / 3) - (104 / 2);/* Top 3ed vertically */
  190.         InitCursor();                                                           /* Make sure it has an arrow cursor */
  191.         theError = DIBadMount(myEvent.where, myEvent.message);/* Let the OS handle the diskette */
  192.         }
  193.  
  194.     }                                                                             /* End of Standard disk inserted handling */
  195. }
  196.  
  197. /* ======================================================= */
  198.  
  199. /* Routine: DoGrow */
  200. /* Purpose: Handle a window resize */
  201.  
  202. void DoGrow(whichWindow)                                                /* Handle a window being resized */
  203. WindowPtr    whichWindow;
  204. {
  205. Rect    OldRect;                                                              /* Window rect before the grow */
  206. Point    myPt;                                                                /* Point for tracking grow box */
  207. Rect    GrowRect;                                                            /* Set the grow bounds */
  208. long    mResult;                                                              /* Result from the grow */
  209.  
  210. if (whichWindow != NIL)                                                     /* See if we have a legal window */
  211.     {
  212.     SetPort(whichWindow);                                                 /* Get ready to draw in this window */
  213.  
  214.     myPt = myEvent.where;                                                 /* Get mouse position */
  215.     GlobalToLocal(&myPt);                                                 /* Make it relative */
  216.  
  217.     OldRect = whichWindow->portRect;                                   /* Save the rect before resizing */
  218.  
  219.     SetRect(&GrowRect, 4, 4, (screenBits.bounds.right - screenBits.bounds.left)-4,  (screenBits.bounds.bottom - screenBits.bounds.top) - 4);/* l,t,r,b */
  220.     mResult = GrowWindow(whichWindow, myEvent.where, &GrowRect);/* Grow it */
  221.     SizeWindow(whichWindow, LoWord(mResult), HiWord(mResult), TRUE);/* Resize to result */
  222.  
  223.  
  224.     SetPort(whichWindow);                                                 /* Get ready to draw in this window */
  225.  
  226.     myPt.h = whichWindow->portRect.right - whichWindow->portRect.left; /* Local right edge */
  227.     myPt.v = whichWindow->portRect.bottom - whichWindow->portRect.top; /* Local bottom edge */
  228.  
  229.     SetRect(&GrowRect, 0, myPt.v - 15, myPt.h + 15, myPt.v + 15); /* Position for horz scrollbar area */
  230.     EraseRect(&GrowRect);                                                 /* Erase old area */
  231.     InvalRect(&GrowRect);                                                 /* Flag us to update it */
  232.  
  233.     SetRect(&GrowRect, myPt.h - 15, 0, myPt.h + 15, myPt.v + 15);  /* Position for vert scrollbar area */
  234.     EraseRect(&GrowRect);                                                 /* Erase old area */
  235.     InvalRect(&GrowRect);                                                 /* Flag us to update it */
  236.  
  237.     DrawGrowIcon(whichWindow);                                         /* Draw the grow Icon again */
  238.     }                                                                             /* End of (WhichWindow <> nil) */
  239. }
  240.  
  241. /* ======================================================= */
  242.  
  243. /* Routine: DoDrag */
  244. /* Purpose: Drag a window around */
  245.  
  246. void DoDrag( whichWindow)                                                /* Handle a window being dragged */
  247. WindowPtr    whichWindow;
  248. {
  249. Rect    OldRect;                                                              /* Window rect before the drag */
  250. Rect    tempRect;                                                            /* temporary rect */
  251.  
  252. OldRect = whichWindow->portRect;                                      /* Save the rect before resizing */
  253.  
  254. tempRect = screenBits.bounds;                                            /* Get screen area,  l,t,r,b, drag area */
  255. SetRect(&tempRect,tempRect.left+4,tempRect.top+4,tempRect.right-4,tempRect.bottom - 4);
  256. DragWindow(whichWindow, myEvent.where, &tempRect);           /* Drag the window */
  257.  
  258.  
  259. }
  260.  
  261. /* ======================================================= */
  262.  
  263. /* Routine: DoGoAway */
  264. /* Purpose: Close a window */
  265.  
  266. void DoGoAway( whichWindow)                                           /* Handle a window goaway box */
  267. WindowPtr    whichWindow;
  268. {
  269. Rect    OldRect;                                                              /* Window rect before the drag */
  270. Rect    tempRect;                                                            /* temporary rect */
  271.  
  272. if (TrackGoAway(whichWindow,myEvent.where) == TRUE)          /* See if mouse released in GoAway box */
  273.     {                                                                             /* Handle the GoAway */
  274.     }                                                                             /* End of TrackGoAway */
  275.  
  276. }
  277.  
  278. /* ======================================================= */
  279.  
  280. /* Routine: DoInContent */
  281. /* Purpose: Pressed in the content area */
  282.  
  283. void DoInContent( whichWindow)                                          /* Handle a hit in the window */
  284. WindowPtr    whichWindow;
  285. {
  286.  
  287. if (whichWindow != FrontWindow())                                      /* See if already selected or not, in front if selected */
  288.     SelectWindow(whichWindow);                                          /* Select this window to make it active */
  289. else                                                                            /* If already in front the already selected */
  290.     {                                                                             /* Handle the press in the content */
  291.     SetPort(whichWindow);                                                 /* Get ready to draw in this window */
  292.     }                                                                             /* End of else */
  293.  
  294. }
  295.  
  296. /* ======================================================= */
  297.  
  298. /* Routine: DoUpdate */
  299. /* Purpose: Got an update event */
  300.  
  301. void DoUpdate()                                                              /* Handle an update to the window */
  302. {
  303. WindowPtr    whichWindow;                                                /* See which window for event */
  304.  
  305. whichWindow = (WindowPtr)myEvent.message;                        /* Get the window the update is for */
  306.  
  307. BeginUpdate(whichWindow);                                               /* Set the clipping to the update area */
  308. EndUpdate(whichWindow);                                                 /* Return to normal clipping area */
  309. }
  310.  
  311. /* ======================================================= */
  312.  
  313. /* Routine: DoActivate */
  314. /* Purpose: Got an activate or deactivate event */
  315.  
  316. void DoActivate()                                                            /* Handle an activate of the window */
  317. {
  318. Boolean    Do_An_Activate;                                                /* Flag to pass */
  319. WindowPtr    whichWindow;                                                /* See which window for event */
  320.  
  321. whichWindow = (WindowPtr)myEvent.message;                        /* Get the window the update is for */
  322.  
  323. Do_An_Activate = ((myEvent.modifiers & 0x0001) != 0);           /* Make sure it is Activate and not DeActivate */
  324. }
  325.  
  326. /* ======================================================= */
  327.  
  328.  
  329.  void main()                                                                  /* Start of main body */
  330. {
  331.  
  332. MoreMasters();                                                             /* This reserves space for more handles */
  333. MaxApplZone();                                                              /* Give us room for memory allocation */
  334. InitGraf(&thePort);                                                         /* Quickdraw Init */
  335. InitFonts();                                                                   /* Font manager init */
  336. InitWindows();                                                               /* Window manager init */
  337. InitMenus();                                                                  /* Menu manager init */
  338. TEInit();                                                                       /* Text edit init */
  339. InitDialogs(NIL);                                                             /* Dialog manager */
  340.  
  341. FlushEvents ( everyEvent , 0 );                                           /* Clear out all events */
  342. InitCursor();                                                                 /* Make an arrow cursor */
  343.  
  344. doneFlag = FALSE;                                                           /* Do not exit program yet */
  345.  
  346. Init_My_Menus();                                                           /* Initialize menu bar */
  347.  
  348. theInput = NIL;                                                               /* Init to no text edit selection active */
  349.  
  350. SleepValue = 40;                                                             /* Set sleep value */
  351. WNE = WNEIsImplemented();                                               /* See if WaitNextEvent is available */
  352.  
  353. UserEventList = NIL;                                                        /* No user events yet */
  354.  
  355. ApplInit_TextMaster();                                                     /* Handle extra program initialization */
  356.  
  357. I_PD_About_Dialog();                                                       /* Initialize the modal dialog globals */
  358. I_PD_Help_window();                                                       /* Initialize the modal dialog globals */
  359. I_PD_Help_window_2();                                                    /* Initialize the modal dialog globals */
  360.  
  361.  
  362. do                                                                              /* Start of main event loop */
  363.     {
  364.  
  365.     ApplLoop_TextMaster();                                                /* Let into main loop */
  366.  
  367.     Handle_User_Event();                                                   /* Check for user events */
  368.  
  369.     if (theInput != NIL)                                                        /* See if a TE is active */
  370.         TEIdle(theInput);                                                      /* Blink the cursor if everything is ok */
  371.  
  372.     if (WNE == TRUE)                                                         /* See if do the MultiFinder way */
  373.         DoIt = WaitNextEvent(everyEvent, &myEvent, SleepValue, NIL);/* Wait for an event */
  374.     else
  375.         {
  376.         SystemTask();                                                         /* For support of desk accessories */
  377.         DoIt = GetNextEvent(everyEvent, &myEvent);                   /* See if an event is ready */
  378.         }
  379.  
  380.     ApplEvent_TextMaster(&DoIt,&myEvent);                          /* Let us at the event first */
  381.  
  382.     if (DoIt == TRUE)                                                         /* If event then... */
  383.         {                                                                         /* Start handling the event */
  384.  
  385.  
  386.         switch (myEvent.what)                                              /* Decide type of event */
  387.             {
  388.             case mouseDown :                                                 /* Mouse button pressed */
  389.                 code = FindWindow(myEvent.where, &whichWindow);/* Get which window the event happened in */
  390.  
  391.                 switch (code)                                                    /* Decide type of event again */
  392.                     {
  393.                     case inMenuBar :                                             /* In the menubar */
  394.                         mResult = MenuSelect(myEvent.where);             /* Do menu selection */
  395.                         theMenu = HiWord(mResult);                           /* Get the menu list number */
  396.                         theItem = LoWord(mResult);                            /* Get the menu list item number */
  397.                         Handle_My_Menu( theMenu, theItem);                /* Handle the menu */
  398.                         break;                                                      /* End of inMenuBar */
  399.  
  400.                     case inDrag :                                                 /* In window drag area */
  401.                         DoDrag(whichWindow);                                  /* Go drag the window */
  402.                         break;                                                      /* End of InDrag */
  403.  
  404.                     case inGrow :                                                 /* In window grow area */
  405.                         DoGrow(whichWindow);                                 /* Handle the growing */
  406.                         break;                                                      /* End of inGrow */
  407.  
  408.                     case inGoAway :                                             /* In window goaway area */
  409.                         DoGoAway(whichWindow);                             /* Handle the goaway button */
  410.                         break;                                                      /* End of inGoAway */
  411.  
  412.                     case inContent :                                             /* In window  contents */
  413.                         DoInContent(whichWindow);                             /* Handle the hit inside a window */
  414.                         break;                                                      /* End of inContent */
  415.  
  416.                     case inSysWindow :                                         /* See if a DA selectio */
  417.                         SystemClick(&myEvent, whichWindow);             /* Let other programs in */
  418.                         break;                                                      /* End of inSysWindow */
  419.  
  420.                     default:                                                        /* Handle others */
  421.                         break;                                                      /* End of otherwise */
  422.                     }                                                                /* End of code case */
  423.                 break;                                                             /* End of MouseDown */
  424.  
  425.             case keyDown:                                                     /* Handle key inputs */
  426.             case autoKey:                                                      /* and auto repeats */
  427.                 DoKeyEvent();                                                   /* Get the key and handle it */
  428.                 break;                                                             /* End of otherwise */
  429.  
  430.             case updateEvt :                                                    /* Update event for a window */
  431.                 DoUpdate();                                                      /* Handle the update */
  432.                 break;                                                             /* End of otherwise */
  433.  
  434.             case diskEvt :                                                      /* Disk inserted event */
  435.                 DoDiskEvent();                                                  /* Handle a disk event */
  436.                 break;                                                             /* End of otherwise */
  437.  
  438.             case activateEvt :                                                 /* Window activated event */
  439.                 DoActivate();                                                    /* Handle the activation */
  440.                 break;                                                             /* End of otherwise */
  441.  
  442.             default:                                                              /* Used for debugging, to see what other events are coming in */
  443.                 break;                                                             /* End of otherwise */
  444.  
  445.             }                                                                      /* End of case */
  446.  
  447.         }                                                                         /* end of GetNextEvent */
  448.     }                                                                             /* end of while */
  449. while (doneFlag == FALSE);                                                /* End of the event loop */
  450.  
  451. ApplExit_TextMaster();                                                    /* Handle extra program termination code */
  452.  
  453. }                                                                                /* end of main */
  454.